package com.it.shiro.service;

import ch.qos.logback.core.joran.util.beans.BeanUtil;
import com.it.shiro.pojo.UserBean;
import com.it.shiro.utils.TestData;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.stream.Collector;
import java.util.stream.Collectors;

/**
 * ClassName:UserService
 * Package:com.it.shiro.service
 * Description: 描述信息
 *
 * @date:2022/3/30 17:00
 * @author:动力节点
 */
@Service
public class UserService {

    @Resource
    private TestData testData;
    //根据用户名查找数据库中的用户
    public UserBean queryUserName(String username){
        UserBean userBean = new UserBean();
        List<Object> queryUsers = testData.getallUsers().stream()
                .filter(user -> username.equals(user.getUserName()))
                .collect(Collectors.toList());
        //查到了数据
        if (queryUsers != null && queryUsers.size()>0){
            try {
                //克隆对象
                userBean= (UserBean) BeanUtils.cloneBean(queryUsers.get(0));
                return userBean;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
            return null;
    }

    public UserBean queryUserByMobile(String userMobile) {
        UserBean userBean = new UserBean();
        List<Object> queryUsers = testData.getallUsers().stream()
                .filter(user -> userMobile.equals(user.getUserMobile()))
                .collect(Collectors.toList());

        //查到了数据
        if (queryUsers != null && queryUsers.size()>0){
            try {
                userBean= (UserBean) BeanUtils.cloneBean(queryUsers.get(0));
                return userBean;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}
